/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-16 20:12:27
 * @LastEditTime: 2022-08-10 09:22:53
 * @LastEditors: liutq
 * @Reference:
 */
/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-16 20:12:27
 * @LastEditTime: 2022-05-19 17:41:31
 * @LastEditors: liutq
 * @Reference:
 */
package middleware

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
)

// JwtAuthMiddleware 登录验证中间件
func JwtAuthMiddleware() func(c *gin.Context) {
	return func(c *gin.Context) {
		authHeader := c.Request.Header.Get("Authorization")
		if authHeader == "" {
			common.ResponseToJson(http.StatusOK, common.NotAuth, "token为空", c)
			c.Abort()
			return
		}
		fmt.Println("token", authHeader)
		reqToken, err := common.ParseToken(authHeader)
		if err != nil {
			common.ResponseToJson(http.StatusOK, common.NotAuth, "token校验失败", c)
			c.Abort()
			return
		}
		c.Set("UserId", reqToken.UserId)
		c.Next()

	}
}
